<?php	
	class LoginController
	{   
		// Copyright Viktor Karlsson , 2011
		// Constructor, private member       
		private $m_database = null; 
		public function __construct(DatabaseConnection $database)
		{
			$this->m_database = $database;  
		} 
		public function DoControll()
		{				
			// Startvariables  
			$xhtml = "";
			$username = "";
			$password = ""; 
			$statusMessage = "";
			      
			// Instances
			$loginView = new LoginView();
			$loginHandler = new LoginHandler($this->m_database);
			$messageLogistics = new MessageLogistics();
			$valid = new Validation();
			
			
			
			// Regular login
      
			 // If trying to logout, do it
			if($loginView->TriedToLogout())
			{
				$loginHandler->DoLogout();
				$statusMessage = $messageLogistics::LoginMessages("LOGGED_OUT");
				echo "<META HTTP-EQUIV='Refresh' Content='0;'>";
			}
			
			$username = $loginView->GetUserName();
			$password = $loginView->GetPassword();
      
			// When user tries to login with input
			if($loginView->TriedToLogIn() || !$username == null && !$password == null && $loginHandler->IsLoggedIn() == false)
			{
				// Collecting data if it isnt set  
				if($username == null && $password == null)
				{
				$username = $loginView->GetUserName();
				$password = $loginView->GetPassword();
				}
 
				// Strip
				$username = strip_tags($username);
				$password = strip_tags($password);

				// Null check
				if(!$username == null && !$password == null)
				{           
				// Username, Password validation below
					if($valid->IsValidUsername($username))
					{
						if($valid->IsValidPassword($password))
						{   
							if($loginHandler->DoLogin($username, $password))
							{
								$statusMessage = $messageLogistics::LoginMessages("SUCCESS_LOGIN", $loginHandler->getUser());
								$loginHandler->SetLastVisitCookie(); 
 
								if($loginView->RememberMe())
								{
									$loginView->SetACookieUser($username);
									$loginView->SetACookiePass($password);
								}
								$_SESSION['redirectUrl'] = htmlentities($_SERVER['REQUEST_URI']);
								$_SESSION['redirectMsg'] = "You have logged in successfully";
								//header("Location: /test/Misc/redirect.php");
								echo "<META HTTP-EQUIV='Refresh' Content='0;'>";
							}
							else
							{
								$statusMessage = $messageLogistics::LoginMessages("FAIL_LOGIN");
							}
						}
						else
						{
							$statusMessage .= $messageLogistics::LoginMessages("INVALID_PASSWORD");
						}
					}
					else
					{
						$statusMessage = $messageLogistics::LoginMessages("INVALID_USERNAME");
					}
				}
				else
				{
					$statusMessage = $messageLogistics::Messages("MISSING_FORM_VALUES");
				}
			}
      
			// If we aint logged in with any options, display login box contents
			// The number identify which type we're logged on with
			if(!$loginHandler->IsLoggedIn())
			{ 				
				// Draws login
				$xhtml .= $loginView->DoLoginBox();
			}
			// If we are logged in, do welcome message, logoutbox
			else if($loginHandler->IsLoggedIn())
			{
				$xhtml .= $loginView->DoLogoutBox($loginHandler->getUser(), $loginHandler->RetriveLastVisitCookie());
			} 
			$xhtmlBuilder = new XhtmlBuilder($xhtml,null,null,$statusMessage);
			return $xhtmlBuilder;  
		}
	}


